package com.zretc.friend.controller;


import com.zretc.api.pojo.User;
import com.zretc.common.entity.Result;
import com.zretc.friend.model.Friend;
import com.zretc.friend.service.FriendService;
import com.zretc.security.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/friends")
public class FriendController {

    @Autowired
    private FriendService friendService;


    @GetMapping("/getFriendList")
    public Result getFriends() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        System.out.println((long)user.getUser().getUserId());
        return Result.success(friendService.getFriends(user.getUser().getUserId()));
    }



    @GetMapping("/follow")
    public Result followUser( Integer friendId) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        friendService.followUser(loginUser.getUser().getUserId(), friendId);
        return Result.success();
    }

    @GetMapping("/unfollow")
    public Result unfollowUser(Integer friendId) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        friendService.unfollowUser(loginUser.getUser().getUserId(), friendId);
        return Result.success();
    }

    @GetMapping ("/block")
    public Result blockUser(Integer friendId) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        friendService.blockUser(loginUser.getUser().getUserId(), friendId);
        return Result.success();
    }

    @GetMapping("/recommend/getUserId")
    public Result recommendFriends() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
//        Friend friend = new Friend();
//        friend.setFriendId(777);
//        friend.setUserId(666);
//        friend.setId(999);
//        friend.setStatus("friend");
        List<Friend> friends=new ArrayList<>();

        for(Integer i=33;i<=40;i++){
            Friend friend = new Friend();
            friend.setFriendId(i);
            friend.setUserId(i+40);
            friend.setId(i+80);
            friend.setStatus("friend");
            friends.add(friend);
        }
        return Result.success(friends);
    }


    @GetMapping("/user/{userId}")
    public Result findUserById(@PathVariable(name = "userId") Integer userId) {
        User user = friendService.findUserById(userId);
        if (user != null) {
            return Result.success(ResponseEntity.ok(user));
        } else {
            return Result.success(ResponseEntity.notFound().build());
        }
    }
}
